﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    public sealed partial class Common
    {
        public static CommonConversionFactor ConversionFactor;
        public static Env Env;
        public static CommonSiteGroup SiteGroup;
        public static CommonGroupUser GroupUser;
        public static CommonStorageCondition StorageCondition;
        public static CommonSupplier Supplier;
        public static CommonUnit Unit;
        public static CommonGlobal Global;
        public static CommonUploadFile UploadFile;
        public static CommonUser User;
        public static Report Report;
        public static Remind Remind;

        public static Func<string> _newId = () => Guid.NewGuid().ToString();

        /// <summary>
        /// 审批流程状态
        /// </summary>
        public enum Status { 草稿, 审批, 发布, 废弃 };

        /// <summary>
        /// 根据键名称获得键值
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static string getGlobalKeyValue(CoreLimsContext dbContext, string KeyName)
        {
            return dbContext.Global.FirstOrDefault(s => s.KeyName == KeyName).KeyValue;
        }

        /// <summary>
        /// 根据名称模糊查询，获得多条记录
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static List<CommonGlobal> getGlobalsContainName(CoreLimsContext dbContext, string KeyName)
        {
            return dbContext.Global.Where(s => s.KeyName.Contains(KeyName)).OrderBy(s => s.KeyName).ToList();
        }

        /// <summary>
        /// 设置键值
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="KeyName"></param>
        /// <param name="KeyValue"></param>
        public static void setGlobalKeyValue(CoreLimsContext dbContext, string KeyName, string KeyValue)
        {
            var record = dbContext.Global.FirstOrDefault(s => s.KeyName == KeyName);
            if (record != null)
            {
                record.KeyValue = KeyValue;
                dbContext.Update(record);
                dbContext.SaveChanges();
            }
        }

        /// <summary>
        /// 服务器端分页返回值辅助类
        /// </summary>
        public class PageHelper
        {
            public Array rows { get; set; }

            public int total { get; set; }
        }
    }

    /// <summary>
    /// 转换因子
    /// </summary>
    public partial class CommonConversionFactor
    {
        public int Id { get; set; }

        /// <summary>
        /// 源单位代码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 转换因子
        /// </summary>
        public decimal Factor { get; set; }

        /// <summary>
        /// 目标单位代码
        /// </summary>
        public string TargetCode { get; set; }
    }

    /// <summary>
    /// 环境监测
    /// </summary>
    public partial class Env
    {
        /// <summary>
        /// 区域和位置定义
        /// </summary>
        public partial class AreaAndLocation
        {
            public AreaAndLocation()
            {
                this.Id = Guid.NewGuid().ToString();
                this.RecordTime = DateTime.Now;
            }

            public string Id { get; set; }

            /// <summary>
            /// 区域或位置名称
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 区域或位置类型
            /// 类型设置为“留样”，则在留样配置和留样管理中可用。
            /// </summary>
            public string Type { get; set; }

            /// <summary>
            /// 区域或位置说明
            /// </summary>
            public string Description { get; set; }

            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime RecordTime { get; set; }

            /// <summary>
            /// 该区域或位置所属父级ID
            /// </summary>
            public string ParentId { get; set; }

            /// <summary>
            /// 区域和位置的环境监测标准
            /// </summary>
            public string EnvMonitoringId { get; set; }

            /// <summary>
            /// 区域和位置的环境监测标准名称
            /// </summary>
            public string EnvMonitoringName { get; set; }

            /// <summary>
            /// 环境监测的时间间隔
            /// </summary>
            public int? Interval { get; set; }

            /// <summary>
            /// 间隔单位。小时、天、周、月、年
            /// </summary>
            public string IntervalUnit { get; set; }

            /// <summary>
            /// 区域与位置的标识，true 为区域，false 为位置
            /// </summary>
            public bool? IsArea { get; set; }

            public enum intervalUnit { 小时, 天, 周, 月 };

            public AreaAndLocation Copy(AreaAndLocation other)
            {
                this.Name = other.Name;
                this.Type = other.Type;
                this.Description = other.Description;
                this.ParentId = other.ParentId;
                this.EnvMonitoringId = other.EnvMonitoringId;
                this.EnvMonitoringName = other.EnvMonitoringName;
                this.Interval = other.Interval;
                this.IntervalUnit = other.IntervalUnit;
                return this;
            }

            /// <summary>
            /// 如果设定了间隔，则必需设置间隔单位。 
            /// </summary>
            /// <returns></returns>
            public bool CheckIntervalUnit 
            { get {
                    var re = true;
                    if (this.Interval != null)
                    {
                        if (this.IntervalUnit.IsNullOrEmpty())
                            return false;
                    }
                    return re;
                }
            }
        }

        /// <summary>
        /// 监测条件标准参照
        /// </summary>
        public partial class Monitoring
        {
            public Monitoring()
            {
                this.Id = Guid.NewGuid().ToString();
            }

            public string Id { get; set; }

            /// <summary>
            /// 标准名称
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 标准类型
            /// </summary>
            public string Type { get; set; }

            /// <summary>
            /// 标准描述
            /// </summary>
            public string Description { get; set; }

            /// <summary>
            /// 温度高限
            /// </summary>
            public decimal? HighTemperature { get; set; }

            /// <summary>
            /// 温度低限
            /// </summary>
            public decimal? LowTemperature { get; set; }

            /// <summary>
            /// 湿度高限
            /// </summary>
            public decimal? HighHumidity { get; set; }

            /// <summary>
            /// 湿度低限
            /// </summary>
            public decimal? LowHumidity { get; set; }

            public Monitoring Copy(Monitoring other)
            {
                this.Name = other.Name;
                this.Type = other.Type;
                this.HighTemperature = other.HighTemperature;
                this.LowTemperature = other.LowTemperature;
                this.HighHumidity = other.HighHumidity;
                this.HighHumidity = other.HighHumidity;
                this.Description = other.Description;
                return this;
            }

        }

        /// <summary>
        /// 环境监测记录表
        /// </summary>
        public partial class MonitoringRecord
        {
            public MonitoringRecord()
            {
                this.Id = Guid.NewGuid().ToString();
                this.RecordTime = DateTime.Now;
                this.Status = false;
                this.IsExamine = false;
            }

            public string Id { get; set; }

            /// <summary>
            /// 关联的区域和位置ID
            /// </summary>
            public string AreaAndLocationId { get; set; }

            /// <summary>
            /// 温度
            /// </summary>
            public decimal? Temperature { get; set; }

            /// <summary>
            /// 湿度
            /// </summary>
            public decimal? Humidity { get; set; }

            /// <summary>
            /// 温度状态，高、合格、低
            /// </summary>
            public string TemStatus { get; set; }

            /// <summary>
            /// 湿度状态，高、合格、低
            /// </summary>
            public string HumStatus { get; set; }

            /// <summary>
            /// 记录人ID
            /// </summary>
            public string RecordPersonId { get; set; }

            /// <summary>
            /// 记录人姓名
            /// </summary>
            public string RecordPersonName { get; set; }

            /// <summary>
            /// 记录时间
            /// </summary>
            public DateTime RecordTime { get; set; }

            /// <summary>
            /// 总体状态，所有状态都合格此项为true，否则为false。
            /// </summary>
            public bool Status { get; set; }

            /// <summary>
            /// 是否需要审核（暂未使用）
            /// </summary>
            public bool IsExamine { get; set; }

            public MonitoringRecord Copy(MonitoringRecord other, Monitoring mon)
            {
                this.AreaAndLocationId = other.AreaAndLocationId;
                this.RecordPersonId = other.RecordPersonId;
                this.RecordPersonName = other.RecordPersonName;

                this.Temperature = other.Temperature;
                this.TemStatus = "合格";
                if (other.Temperature > mon.HighTemperature)
                    this.TemStatus = "高";
                if (other.Temperature < mon.LowTemperature)
                    this.TemStatus = "低";

                this.Humidity = other.Humidity;
                this.HumStatus = "合格";
                if (other.Humidity > mon.HighHumidity)
                    this.HumStatus = "高";
                if (other.Humidity < mon.LowHumidity)
                    this.HumStatus = "低";

                if (this.TemStatus == "合格" && this.HumStatus == "合格")
                    this.Status = true;
                else
                    this.Status = false;

                return this;
            }

        }
    }

    /// <summary>
    /// 实验室和工作组
    /// </summary>
    public partial class CommonSiteGroup
    {
        public string Id { get; set; }

        /// <summary>
        /// 实验室
        /// </summary>
        public string Site { get; set; }

        /// <summary>
        /// 工作组
        /// </summary>
        public string WorkGroup { get; set; }

        /// <summary>
        /// 说明
        /// </summary>
        public string Description { get; set; }
    }

    /// <summary>
    /// 工作组用户
    /// </summary>
    public partial class CommonGroupUser
    {
        public CommonGroupUser() { 
            this.Id = Guid.NewGuid().ToString();
        }
        public string Id { get; set; }

        public string GroupId { get; set; }

        public string UserId { get; set; }
    }

    /// <summary>
    /// 稳定性研究存储条件（暂未使用）
    /// </summary>
    public partial class CommonStorageCondition
    {
        /// <summary>
        /// 条件名称
        /// </summary>
        public string ConditionName { get; set; }

        /// <summary>
        /// 最低温度
        /// </summary>
        public string MinTemperature { get; set; }

        /// <summary>
        /// 最高温度
        /// </summary>
        public string MaxTemperature { get; set; }

        /// <summary>
        /// 标准温度
        /// </summary>
        public string StdTemperature { get; set; }

        /// <summary>
        /// 湿度
        /// </summary>
        public string Humidity { get; set; }

        /// <summary>
        /// 光度
        /// </summary>
        public string Iuminosity { get; set; }

        /// <summary>
        /// 其它
        /// </summary>
        public string Other { get; set; }

        /// <summary>
        /// 时间间隔
        /// </summary>
        public int? Interval { get; set; }

        /// <summary>
        /// 间隔单位。小时、天、周、月、年
        /// </summary>
        public string IntervalUnit { get; set; }

        public enum intervalUnit { 小时, 天, 周, 月, 年 };
    }

    /// <summary>
    /// 供应商
    /// </summary>
    public partial class CommonSupplier
    {
        public string SupplierId { get; set; }

        /// <summary>
        /// 供应商代码/简称
        /// </summary>
        public string SupplierCode { get; set; }

        /// <summary>
        /// 供应商名称
        /// </summary>
        public string SupplierName { get; set; }

        /// <summary>
        /// 供应商类型 比如：原材料、仪器设备、办公耗材、生物制药等区分供应商类别
        /// </summary>
        public string SupplierType { get; set; }

        /// <summary>
        /// 地址1
        /// </summary>
        public string Address1 { get; set; }

        /// <summary>
        /// 地址2
        /// </summary>
        public string Address2 { get; set; }

        /// <summary>
        /// 国家
        /// </summary>
        public string Nation { get; set; }

        /// <summary>
        /// 省份
        /// </summary>
        public string Province { get; set; }

        /// <summary>
        /// 城市
        /// </summary>
        public string City { get; set; }

        /// <summary>
        /// 邮编
        /// </summary>
        public string ZipCode { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        public string Telephone { get; set; }

        /// <summary>
        /// 传真
        /// </summary>
        public string Fax { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string Contacts { get; set; }

        /// <summary>
        /// 联系人电话
        /// </summary>
        public string ContactTelephone { get; set; }

        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 公司网址
        /// </summary>
        public string Website { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Note { get; set; }
    }

    /// <summary>
    /// 单位
    /// </summary>
    public partial class CommonUnit
    {
        [Required(ErrorMessage = "单位代码必须填写")]
        /// <summary>
        /// 单位代码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 单位类型，重量、体积、热力学单位等
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 英文名称
        /// </summary>
        public string EnglishName { get; set; }

        /// <summary>
        /// 中文名称
        /// </summary>
        public string ChineseName { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Description { get; set; }
    }

    /// <summary>
    /// 全局变量表
    /// </summary>
    public partial class CommonGlobal
    {
        public string KeyName { get; set; }
        public string KeyValue { get; set; }
        public string Description { get; set; }

    }

    /// <summary>
    /// 上传文件管理表
    /// </summary>
    public partial class CommonUploadFile
    {
        public CommonUploadFile()
        {
            this.Id = Common._newId();
        }

        public string Id { get; set; }

        public string Domain { get; set; }

        public string DomainId { get; set; }

        /// <summary>
        /// 原文件名，下载时使用该名称。
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 文件类型
        /// </summary>
        public string FileType { get; set; }

        public string FileSize { get; set; }

        /// <summary>
        /// 上传时间
        /// </summary>
        public DateTime UploadTime { get; set; }

        /// <summary>
        /// 上传人ID
        /// </summary>
        public string CreateById { get; set; }

        /// <summary>
        /// 上传人姓名
        /// </summary>
        public string CreateByName { get; set; }
    }

    /// <summary>
    /// 用户基本信息
    /// </summary>
    public partial class CommonUser
    {
        public string Id { get; set; }

        /// <summary>
        /// 对应 Identity 中的 Name
        /// </summary>
        public string IdentityName { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string FullName { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public string Sex { get; set; }

        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime? DateOfBirth { get; set; }

        /// <summary>
        /// 入职日期
        /// </summary>
        public DateTime? EmploymentDate { get; set; }

        /// <summary>
        /// 政治面貌
        /// </summary>
        public string Politic { get; set; }

        /// <summary>
        /// 民族
        /// </summary>
        public string Nation { get; set; }

        /// <summary>
        /// 籍贯
        /// </summary>
        public string NativePlace { get; set; }

        /// <summary>
        /// 专业
        /// </summary>
        public string Major { get; set; }

        /// <summary>
        /// 学历
        /// </summary>
        public string Education { get; set; }

        /// <summary>
        /// 毕业院校
        /// </summary>
        public string WentToSchool { get; set; }

        /// <summary>
        /// 家庭住址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        public string PhoneNumber { get; set; }

        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 职位
        /// </summary>
        public string JobTitle { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        public string Head { get; set; }

        /// <summary>
        /// 签名
        /// </summary>
        public string Signature { get; set; }

        /// <summary>
        /// 工作经历
        /// </summary>
        public string WorkExperience { get; set; }

        /// <summary>
        /// 教育经历
        /// </summary>
        public string EducationalBackground { get; set; }

        /// <summary>
        /// 技能证书
        /// </summary>
        public string SkillCertificate { get; set; }

        /// <summary>
        /// 自我介绍
        /// </summary>
        public string SelfIntroduction { get; set; }

        /// <summary>
        /// 奖惩情况
        /// </summary>
        public string Rewards { get; set; }
}

    /// <summary>
    /// 待办提醒表 系统中所有待办提醒都放在此处
    /// </summary>
    public partial class Remind
    {
        public Remind() 
        {
            this.Id = Common._newId();
            this.CreateTime = DateTime.Now;
        }
        public string Id { get; set; }

        /// <summary>
        /// 被提醒组ID
        /// </summary>
        public string GroupId { get; set; }

        /// <summary>
        /// 被提醒的人ID
        /// </summary>
        public string UserId { get; set; }

        /// <summary>
        /// 被提醒人姓名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 该提醒创建的时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 提醒内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 提醒标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 该提醒对应的流程ID
        /// </summary>
        public string WorkFlowId { get; set; }

        /// <summary>
        /// 该提醒被读取/处理的时间
        /// </summary>
        public DateTime? ReadTime { get; set; }

        /// <summary>
        /// 读取/处理提醒的人ID（该项只对提醒组起作用，此处记录组中读取/处理该提醒的人）
        /// </summary>
        public string ReadUserId { get; set; }

        /// <summary>
        /// 发送提醒的人ID
        /// </summary>
        public string SendRemindId { get; set; }

        /// <summary>
        /// 发送提醒人姓名
        /// </summary>
        public string SendRemindName { get; set; }
    }

    /// <summary>
    /// 后台生成各种标签及报告的处理类
    /// </summary>
    public class Report
    {
    }

}
